package base64;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class Base64GUI extends JDialog {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        runGUI();
      }
    });
  }
  
  private static void runGUI(){
    Base64GUI gui = new Base64GUI();
    gui.setVisible(true);
  }

  private JTextField base64Field;
  
  private JTextField sourceField;
  
  public Base64GUI() {
    super();
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
      e.printStackTrace();
    }
    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    base64Field = new JTextField(40);
    sourceField = new JTextField(40);
    setLayout(new GridBagLayout());
    JButton encodeButton = new JButton("enc");
    JButton decodeButton = new JButton("dec");
    JButton clearButton = new JButton("clear");
    add(sourceField, new GridBagConstraints(0, 0, GridBagConstraints.REMAINDER, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    add(encodeButton, new GridBagConstraints(0, 1, 1,1,1,0,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(2,2,2,2),0,0));
    add(decodeButton, new GridBagConstraints(1, 1, 1,1,0,0,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2,2,2,2),0,0));
    add(clearButton, new GridBagConstraints(2, 1, 1,1,1,0,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2,2,2,2),0,0));
    add(base64Field, new GridBagConstraints(0, 2, GridBagConstraints.REMAINDER, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
    clearButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        clear();
      }
    });
    encodeButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent e) {
        encode();
      }
    });
    decodeButton.addActionListener(new ActionListener(){
      @Override
      public void actionPerformed(ActionEvent e) {
        decode();
      }
    });
    pack();
  }

  private void encode(){
    String text = sourceField.getText();
    byte[] bytes = text.getBytes();
    try{
      String base64 = Base64Reader.toBase64(bytes);
      base64Field.setText(base64);
    } catch (IOException ioe){
      base64Field.setText(ioe.getMessage());
    }
  }
  
  private void decode(){
    String base64 = base64Field.getText();
    byte[] bytes;
    try {
      bytes = Base64Writer.toBytes(base64);
      String text = new String(bytes);
      sourceField.setText(text);
    } catch (IOException e) {
      sourceField.setText(e.getMessage());
    }
  }

  private void clear(){
    base64Field.setText("");
    sourceField.setText("");
  }
}
